package com.genesis.yunnanji.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.genesis.yunnanji.view.DataLoadingDialog;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import es.dmoral.toasty.Toasty;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    public static final String WEIXIN_APPID = "wx9c0c7b1f3addd037";

    public static final String WEIXIN_APPSECRET = "190afc6e66e55fcb3478418395a5e899";

    public static final String WXACCESS_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token";

    public static final String WXUSER_INFO = "https://api.weixin.qq.com/sns/userinfo";

    private IWXAPI api;
    private DataLoadingDialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialog = new DataLoadingDialog(this);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
//        api = WXAPIFactory.createWXAPI(this, "wx9cf409e205703d74");//以前的
        api = WXAPIFactory.createWXAPI(this, "wx9c0c7b1f3addd037");
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Log.e("lk", "onResp: " + resp.errCode);
            if (resp.errCode == 0) {
                EventBus.getDefault().post(new String("success"));
                Toasty.success(this, "支付成功！", Toast.LENGTH_SHORT).show();
                this.finish();
            } else if (resp.errCode == -2) {
                Toasty.info(this, "您已取消支付！", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toasty.error(this, "支付失败！", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

}